/*
 *
 * @Date: 2022-06-09 19:42:19
 * @LastEditTime: 2022-06-19 20:49:29
 * @LastEditors:
 */
import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {HttpService} from '../../service/http.service';
import {Location} from '@angular/common';
import {AuthService} from '../../service/auth.service';
import {ApiPath} from '../../config/api-path';
import {UserInfo} from '../../model/userInfo';
import {Gender} from '../../enum/gender';
import {Campus} from '../../enum/campus';
import {Education} from '../../enum/education';
import {AlertController} from '@ionic/angular';

@Component({
  selector: 'app-info-container',
  templateUrl: './info-container.component.html',
  styleUrls: ['./info-container.component.scss'],
})
export class InfoContainerComponent implements OnInit {
  constructor(
    private router: Router,
    private HttpSrv: HttpService,
    private location: Location,
    private authService: AuthService,
    private alertCtrl: AlertController
  ) {
  }


  userInfo: UserInfo = {
    stuId: '',
    userName: '',
    stuCardUrl: '',
    userGender: '',
    campus: '',
    dormitory: '',
    roomNum: '',
    educationLevel: '',
    grade: 1,
    college: '',
    major: '',
    phoneNum: '',
    qqNum: '',
    wechatAccount: '',
  };
  userInfoEnum: UserInfo = {
    stuId: '',
    userName: '',
    stuCardUrl: '',
    userGender: '',
    campus: '',
    dormitory: '',
    roomNum: '',
    educationLevel: '',
    grade: 0,
    college: '',
    major: '',
    phoneNum: '',
    qqNum: '',
    wechatAccount: '',
  };
  isLogin: Boolean;

  ngOnInit() {
    this.initi();
  }

  initi() {
    this.authService.isLogin.subscribe(r => {
      this.isLogin = r;
      console.debug(r);
    });
    if (this.isLogin) {
      this.getUsrInfo();
    }
  }

  gotoLogin() {
    this.router.navigate(['user/login']).then(r => console.debug('r', r));
  }

  /**
   * @description: 返回上一页
   * @return
   */
  onLeftClick() {
    this.location.back();
  }

  /**
   * @description: 查询用户信息
   */
  getUsrInfo() {
    this.HttpSrv.httpGet<UserInfo>(ApiPath.request.user.getInfo)
      .subscribe(res => {
        console.debug('res', res);
        Object.assign(this.userInfo, res.data);
        Object.assign(this.userInfoEnum, res.data);
        this.userInfoEnum = res.data;
        this.userInfo.educationLevel = Education[res.data.educationLevel];
        this.userInfo.campus = Campus[res.data.campus];
        this.userInfo.userGender = Gender[res.data.userGender];
        console.debug('this.userInfo', this.userInfo);
      });
  }


  saveUserInfo() {
    let msg = '出错了！请稍后再试！';
    console.debug(Campus[this.userInfo.campus]);
    console.debug(this.userInfo.campus);
    Object.assign(this.userInfoEnum, this.userInfo);
    this.userInfoEnum.campus = Campus[this.userInfo.campus].toString();
    this.userInfoEnum.educationLevel = Education[this.userInfo.educationLevel].toString();
    this.userInfoEnum.userGender = Gender[this.userInfo.userGender].toString();
    console.debug('userInfoEnum', this.userInfoEnum);
    this.HttpSrv.httpPost<UserInfo>(ApiPath.request.user.changeInfo, this.userInfoEnum)
      .subscribe(
        res => {
          console.debug(res);
          if (res.code === 0) {
            msg = '修改成功！';
          } else {
            if (res && res.hasOwnProperty('msg')) {
              msg = res.msg;
            }
          }
          this.showAlert(msg).then(r => null);
        });
  }

  lougout() {
    this.authService.delToken();
  }

  gotoAdvice() {
    this.router.navigate(['/create',"advice"]).then(r => console.debug('r', r));
  }

  async showAlert(msg: string) {
    const alert = await this.alertCtrl.create({
      message: msg,
      buttons: ['确定']
    });
    await alert.present();
  }

}
